<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Qwadmin\Controller;
class TakeMoneyController extends ComController {
    
    public function index() {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = 20; #每页数量
        $where['cw.status'] = 0;
        if (I('get.status')) {
            $where['cw.status'] = I('get.status');
        }
        if (I('get.customer_id')) {
            $where['cw.customer_id'] = I('get.customer_id');
        }
        $count = M('customer_withdrawals')->join('JOIN zt_customer as c ON c.id = cw.customer_id')->join('JOIN zt_customer_take_account as cta ON cta.id = cw.account_id')
                                         ->alias('cw')->where($where)->count();
        $list = M('customer_withdrawals')->join('JOIN zt_customer as c ON c.id = cw.customer_id')->join('JOIN zt_customer_take_account as cta ON cta.id = cw.account_id')
                                         ->alias('cw')->field('cw.*,cta.account_no,cta.account_name,c.shop_name')->order('cw.id DESC')->where($where)->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('total' , M('customer_withdrawals')->count());
        $this->assign('w_total' , M('customer_withdrawals')->where(array('status' => 0))->count());
        $this->assign('s_total' , M('customer_withdrawals')->where(array('status' => 1))->count());
        $this->assign('c_total' , M('customer_withdrawals')->where(array('status' => 2))->count());
        $this->assign('p_total' , M('customer_withdrawals')->where(array('status' => 2))->sum('money'));
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }
    
    
    public function saveStatus() {
        $id = I('get.id') && preg_match('/^[0-9]+((\,[0-9]+)+)?/is', I('get.id')) ? I('get.id') : $this->error('发生了一个错误！');
        $status = !in_array(I('get.status'), array(1, 2)) ? $this->error('提现状态错误') : I('get.status');
        $note = I('get.note') ? I('get.note') : '';
	$data_save = array('status' => $status, 'feedback' => $note);
        if (M('customer_withdrawals')->where(array('id' => array('in', $id)))->save( $data_save ) === flase) {
            $this->error('提现失败');
        } else {
            $obj = explode(',' , $id);
            foreach( $obj as $key => $value ) {
                $account_info = M('customer_withdrawals')->find( $value );
                $make_account = M('customer_take_account')->find( $account_info['account_id'] );
                $status == 2 ? M('customer_take_account')->where(array('id' => $account_info['account_id']))->save(array('arrival_amount' => $make_account['arrival_amount'] + $account_info['money'])) : '';
                system_message('本地账户提现反馈' , "您于【". date( "Y-m-d H:i:s", $account_info['create_time'])."】申请的【{$account_info['money']}】元，后台审核提现".($status == 1?'失败':'成功')."，详情请查看提现列表。" , $account_info['customer_id']);
            }
            $this->success('提现成功');
        }
    }
}